// 数组转树形结构;
// 非递归
export const arrayToTree: any = (
  arr: any[],
  idAttr = 'id',
  pidAttr = 'pid'
) => {
  const result: any[] = [];
  if (!Array.isArray(arr) || arr.length === 0) {
    return result;
  }
  const map: any = {};
  arr.forEach((item) => (map[item[idAttr]] = item));
  arr.forEach((item) => {
    const parent = map[item[pidAttr]];
    if (parent) {
      (parent.children || (parent.children = [])).push(item);
    } else {
      result.push(item);
    }
  });
  return result;
};
// 数组转树形结构;
// 递归
export const recursionArrayToTree = (
  arr: any[],
  pid: any,
  idAttr = 'id',
  pidAttr = 'pid'
) => {
  const res: any[] = [];
  arr.forEach((item) => {
    if (item[pidAttr] === pid) {
      const itemChildren = recursionArrayToTree(arr, item[idAttr]);
      if (itemChildren.length) {
        item.children = itemChildren;
      }
      res.push(item);
    }
  });
  return res;
};

// 数组扁平化
export const flatten = (arr: any[]) => {
  let res: any[] = [];
  arr.forEach((item) => {
    if (Array.isArray(item)) {
      res = res.concat(flatten(item));
    } else {
      res.push(item);
    }
  });
  return res;
};

// 查找某一节点在树中路径
export const getNodePath = (tree: any[], id: any, idAttr = 'id') => {
  if (!Array.isArray(tree) || tree.length === 0) {
    return [];
  }
  const path: never[] = [];
  const treeFindPath: any = (treeCopy: any[], idCopy: any, pathCopy: any[]) => {
    for (const item of treeCopy) {
      pathCopy.push(item[idAttr]);
      if (item[idAttr] === idCopy) {
        return pathCopy;
      }
      if (item.children) {
        const findChildren = treeFindPath(item.children, idCopy, pathCopy);
        if (findChildren.length) {
          return findChildren;
        }
      }
      pathCopy.pop();
    }
    return [];
  };
  return treeFindPath(tree, id, path);
};

// 模糊查询树
export const fuzzyQueryTree = (arr: any[], value: any) => {
  if (!Array.isArray(arr) || arr.length === 0) {
    return [];
  }
  const result: any[] = [];
  arr.forEach((item) => {
    if (item.name.indexOf(value) > -1) {
      const children = fuzzyQueryTree(item.children, value);
      const obj = { ...item, children };
      result.push(obj);
    } else {
      if (item.children && item.children.length > 0) {
        const children = fuzzyQueryTree(item.children, value);
        const obj = { ...item, children };
        if (children && children.length > 0) {
          result.push(obj);
        }
      }
    }
  });
  return result;
};

//节点添加属性
export const addAttrToNodes = (
  tree: any[],
  attr: string | number,
  value: any
) => {
  tree.forEach((item: any) => {
    item[attr] = value;
    if (item.children && item.children.length > 0) {
      addAttrToNodes(item.children, attr, value);
    }
  });
  return tree;
};

// 删除空树
export const removeEmptyChildren = (tree: any[]) => {
  tree.forEach((item: any) => {
    if (item.children && item.children.length === 0) {
      delete item.children;
    } else if (item.children && item.children.length > 0) {
      removeEmptyChildren(item.children);
    }
  });
  return tree;
};

// 获取所有叶子节点
export const getAllLeaf = (tree: any) => {
  const result: any[] = [];
  const getLeaf = (tree: any[]) => {
    tree.forEach((item: any) => {
      if (!item.children) {
        result.push(item);
      } else {
        getLeaf(item.children);
      }
    });
  };
  getLeaf(tree);
  return result;
};
